package com.www.applyword.ui.chat;

import android.Manifest;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.JobAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.BookFriendBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/11/16 0016 14:42
 * version: 1.0
 */
public class BookFriendsActivity extends BaseActivity {
    @BindView(R.id.rv_new_friends)
    RecyclerView rvNewFriends;
    private JobAdapter mHotJobAdapter;
    private List<String> phoneNumberFromMobile;
    private String KeywordList = "";

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.book_friend_activity);
        setTitle("通讯录好友");
    }

    @Override
    protected void loadData() {
        rvNewFriends.setLayoutManager(new LinearLayoutManager(BookFriendsActivity.this));
        rvNewFriends.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new JobAdapter(BookFriendsActivity.this);
        rvNewFriends.setAdapter(mHotJobAdapter);


        RxPermissions rxPermissions = new RxPermissions(BookFriendsActivity.this);
        rxPermissions
                .request(Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)
                .subscribe(granted -> {
                    if (granted) {
                        phoneNumberFromMobile = Utils.getPhoneNumberFromMobile(BookFriendsActivity.this);
                        if (null != phoneNumberFromMobile && phoneNumberFromMobile.size() > 0){
                            for (String s : phoneNumberFromMobile) {
                                KeywordList += s + ",";
                            }

                            Map<String, Object> params = new HashMap<>();
                            params.put("KeywordList", KeywordList.substring(0, KeywordList.length() - 1));
                            params.put("Type", "1");
                            params.put("UserID", SPUtil.getString(Constants.USERID));
                            HttpClient.post(BookFriendsActivity.this, Constants.GETMESSAGEUSERLIST, params, new CallBack<List<BookFriendBean>>() {
                                @Override
                                public void onSuccess(List<BookFriendBean> bookFriendBeans) {
                                    if (bookFriendBeans.size() == 0){
                                        showEmpty(true, vsEmpty, 4);
                                    }
                                    mHotJobAdapter.replaceAll(bookFriendBeans,true);

                                }
                            });
                        }else {
                            showEmpty(true, vsEmpty, 0);
                        }
                    } else {

                    }
                });
    }
}
